<?php

namespace app\admin\controller;
use think\facade\Request;
use think\Controller;
use think\facade\Session;
use think\facade\DB;
use app\admin\model\Admin as Admin;

class Login extends Controller
{

    public function login()
    {
        if(request()->isGet()){
           $this->view->engine->layout(false);
        return $this->fetch(); 
    }else{
        
            $data=Request::param();
            $rule=[
                'username|用户名'=>'require',
                'password|密码'=>'require',
            ];


            $message=[
                'username'=>'用户名不能为空',
                'password'=>'密码不能为空',

            ];
            $validate=new \think\Validate($rule,$message);
            if($validate->check($data)){
                $result=Admin::where('username',$data['username'])
                ->where('password',$data['password'])
                ->find();
                
                    if($result){
                      Session::set('name',$result->toArray());
                    $this->success('登录成功','admin/index/index');  
                    }else{
                    return  $this->error('用户名或者密码错误','/admin/login/login');
                    }

                    
                
            }else{
             return   $this->error($validate->getError(),'/admin/login/login');
            }
        
        }
    }
 

}
